package com.iscale.controller;

import com.iscale.model.entity.BaseBrandInfo;
import com.iscale.model.entity.BaseFileInfo;
import com.iscale.model.entity.BaseNeedsInfo;
import com.iscale.service.BaseFileInfoService;
import com.iscale.service.BaseNeedsInfoService;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.iscale.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 需求类别 前端控制器
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Api(value = "品类管理模块",description = "品类管理模块")
@Controller
@RequestMapping("/baseNeedsInfo")
public class BaseNeedsInfoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(BaseBrandInfoController.class);

    @Autowired
    BaseNeedsInfoService needsInfoService;

    @Autowired
    BaseFileInfoService fileInfoService;

    @ApiOperation(value = "添加品类", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "saveNeeds", method = RequestMethod.POST)
    public Result saveNeeds(
            @ApiParam(value = "名称", required = true) @RequestParam(defaultValue = "", required = true) String name,
            @ApiParam(value = "商户编号", required = false) @RequestParam(defaultValue = "", required = false) String merchantNo,
            @ApiParam(value = "父ID", required = false) @RequestParam(defaultValue = "", required = false) Long pid,
            @ApiParam(value = "状态", required = false) @RequestParam(defaultValue = "", required = false) Integer status,
            @ApiParam(value = "是否可删除", required = false) @RequestParam(defaultValue = "", required = false) Integer isFinal,
            @ApiParam(value = "Logo", required = false) @RequestParam(required = false) MultipartFile file
    ) throws Exception {
        BaseNeedsInfo needsInfo = new BaseNeedsInfo(name,merchantNo,pid,"","",status,isFinal);

        Result result =  needsInfoService.saveNeeds(file,needsInfo);

        return result;
    }

    @ApiOperation(value = "修改品类", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "updateNeeds", method = RequestMethod.POST)
    public Result updateNeeds(
            @ApiParam(value = "ID", required = true) @RequestParam(defaultValue = "", required = true) String id,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "", required = false) String name,
            @ApiParam(value = "商户编号", required = false) @RequestParam(defaultValue = "", required = false) String merchantNo,
            @ApiParam(value = "父ID", required = false) @RequestParam(defaultValue = "", required = false) Long pid,
            @ApiParam(value = "状态", required = false) @RequestParam(defaultValue = "", required = false) Integer status,
            @ApiParam(value = "是否可删除", required = false) @RequestParam(defaultValue = "", required = false) Integer isFinal,
            @ApiParam(value = "Logo", required = false) @RequestParam(required = false) MultipartFile file
    ) throws Exception {
        BaseNeedsInfo needsInfo = new BaseNeedsInfo(name,merchantNo,pid,"","",status,isFinal);

        Result result =  needsInfoService.updateNeeds(file,needsInfo);

        return result;
    }

    @ApiOperation(value = "查询大品类", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectBigNeeds", method = RequestMethod.POST)
    public Result selectBigNeeds() throws Exception {
        List<BaseNeedsInfo> ls = needsInfoService.selectBigNeeds();

        return Result.success(ls);
    }
}
